package com.black.order.controller;

import com.alibaba.csp.sentinel.annotation.SentinelResource;
import com.alibaba.csp.sentinel.slots.block.BlockException;
import com.black.order.service.OrderService;
import com.black.order.bean.Order;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

/**
 * @Author: Young
 * @CreateTime: 2025-07-29
 * @Description:
 * @Version: 1.0
 */
@RestController
@RequestMapping("/api/order")
public class OrderController {

    @Autowired
    OrderService orderService;

    @GetMapping("/create")
    public Order createOrder(@RequestParam Long userId,
                             @RequestParam Long productId){
        Order order = orderService.createOrder(userId, productId);
        return order;
    }

    @GetMapping("/seckill")
    @SentinelResource(value = "seckill-order",fallback = "seckillFallback")
    public Order seckill(@RequestParam(value = "userId",defaultValue = "-1") Long userId,
                         @RequestParam(value = "productId",defaultValue = "-1") Long productId){
        Order order = orderService.createOrder(userId, productId);
        return order;
    }

    public Order seckillFallback(Long userId, Long productId, BlockException blockException){
        Order order = new Order();
        order.setId(-1L);
        order.setNickName("秒杀失败");
        return order;
    }

    @GetMapping("/writeDb")
    public String writeDb(){
        return "write success";
    }

    @GetMapping("/readDb")
    public String readDb(){
        return "read success";
    }
}
